#include <mysql_driver.h>
#include <mysql_connection.h>
#include <cppconn/statement.h>
#include <cppconn/prepared_statement.h>
#include <cppconn/resultset.h>
#include <cppconn/exception.h>
#include <iostream>
#include <string>
#include <vector>

using namespace std;
using namespace sql;

class TodoApp {
private:
    unique_ptr<Connection> con;
    const string server = "localhost";
    const string user = "your_username";
    const string password = "your_password";
    const string database = "todo_db";

public:
    TodoApp() {
        try {
            sql::mysql::MySQL_Driver *driver;
            driver = sql::mysql::get_mysql_driver_instance();
            con.reset(driver->connect(server, user, password));
            con->setSchema(database);
        } catch (SQLException &e) {
            cerr << "数据库连接错误: " << e.what() << endl;
            cerr << "SQLState: " << e.getSQLState() << endl;
            cerr << "错误代码: " << e.getErrorCode() << endl;
            exit(EXIT_FAILURE);
        }
    }

    void createTable() {
        try {
            unique_ptr<Statement> stmt(con->createStatement());
            stmt->execute("CREATE TABLE IF NOT EXISTS todos ("
                         "id INT AUTO_INCREMENT PRIMARY KEY,"
                         "task VARCHAR(255) NOT NULL,"
                         "completed BOOLEAN DEFAULT false,"
                         "created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP)");
            cout << "表创建成功或已存在" << endl;
        } catch (SQLException &e) {
            cerr << "创建表错误: " << e.what() << endl;
        }
    }

    void addTask(const string& task) {
        try {
            unique_ptr<PreparedStatement> pstmt(con->prepareStatement(
                "INSERT INTO todos (task) VALUES (?)"));
            pstmt->setString(1, task);
            pstmt->execute();
            cout << "任务添加成功" << endl;
        } catch (SQLException &e) {
            cerr << "添加任务错误: " << e.what() << endl;
        }
    }

    void listTasks() {
        try {
            unique_ptr<Statement> stmt(con->createStatement());
            unique_ptr<ResultSet> res(stmt->executeQuery("SELECT * FROM todos"));
            
            cout << "\n=== 任务列表 ===\n";
            while (res->next()) {
                cout << "[" << (res->getBoolean("completed") ? "✓" : " ") << "] "
                     << res->getInt("id") << ". "
                     << res->getString("task") << " ("
                     << res->getString("created_at") << ")" << endl;
            }
            cout << "===============\n";
        } catch (SQLException &e) {
            cerr << "列出任务错误: " << e.what() << endl;
        }
    }

    void markTaskCompleted(int id) {
        try {
            unique_ptr<PreparedStatement> pstmt(con->prepareStatement(
                "UPDATE todos SET completed = true WHERE id = ?"));
            pstmt->setInt(1, id);
            int rows = pstmt->executeUpdate();
            if (rows > 0) {
                cout << "任务标记为已完成" << endl;
            } else {
                cout << "未找到该任务ID" << endl;
            }
        } catch (SQLException &e) {
            cerr << "更新任务错误: " << e.what() << endl;
        }
    }

    void deleteTask(int id) {
        try {
            unique_ptr<PreparedStatement> pstmt(con->prepareStatement(
                "DELETE FROM todos WHERE id = ?"));
            pstmt->setInt(1, id);
            int rows = pstmt->executeUpdate();
            if (rows > 0) {
                cout << "任务已删除" << endl;
            } else {
                cout << "未找到该任务ID" << endl;
            }
        } catch (SQLException &e) {
            cerr << "删除任务错误: " << e.what() << endl;
        }
    }
};

int main() {
    TodoApp app;
    app.createTable();

    while (true) {
        cout << "\n=== Todo 应用 ===\n";
        cout << "1. 添加任务\n";
        cout << "2. 查看任务列表\n";
        cout << "3. 标记任务为已完成\n";
        cout << "4. 删除任务\n";
        cout << "5. 退出\n";
        cout << "请选择: ";

        int choice;
        cin >> choice;
        cin.ignore(); // 忽略换行符

        switch (choice) {
            case 1: {
                cout << "输入任务内容: ";
                string task;
                getline(cin, task);
                app.addTask(task);
                break;
            }
            case 2:
                app.listTasks();
                break;
            case 3: {
                cout << "输入要标记的任务ID: ";
                int id;
                cin >> id;
                app.markTaskCompleted(id);
                break;
            }
            case 4: {
                cout << "输入要删除的任务ID: ";
                int id;
                cin >> id;
                app.deleteTask(id);
                break;
            }
            case 5:
                cout << "退出应用..." << endl;
                return 0;
            default:
                cout << "无效选择，请重试" << endl;
        }
    }
}
    